package com.sc.byahi.user.client.components.maps;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.HandlerRegistration;

public class MapImpl extends MVCObject<MapImpl> {

	protected MapImpl() {
	}

	public final static MapImpl newInstance(Element element, MapOptions options) {
		return createJso(element, options);
	}

	private final static native MapImpl createJso(Element element, MapOptions options) /*-{
		return new $wnd.google.maps.Map(element, options);
	}-*/;

	public final native LatLng getCenter() /*-{
		return this.getCenter();
	}-*/;

	public final native Element getDiv() /*-{
		return this.getDiv();
	}-*/;

	public final native int getZoom() /*-{
		return this.getZoom();
	}-*/;

	public final native void panTo(LatLng latLng) /*-{
		this.panTo(latLng);
	}-*/;

	public final native void setCenter(LatLng latlng) /*-{
		this.setCenter(latlng);
	}-*/;

	public final native void setOptions(MapOptions options) /*-{
		this.setOptions(options);
	}-*/;

	public final native void setZoom(int zoom) /*-{
		this.setZoom(zoom);
	}-*/;

	public final native void panToBounds(LatLngBounds latLngBounds) /*-{
		this.panToBounds(latLngBounds);
	}-*/;

	public final native void fitBounds(LatLngBounds viewport) /*-{
	      this.fitBounds(viewport);
	}-*/;

	
	public final native void triggerResize() /*-{
		$wnd.google.maps.event.trigger(this, 'resize');
	}-*/;

	public final HandlerRegistration addClickHandler(MClickHandler handler) {

		final JavaScriptObject listener = addHandlerImpl(this, "click", handler);

		HandlerRegistration registration = new HandlerRegistration() {
			@Override
			public void removeHandler() {
				removeHandlerImpl(listener);
			}
		};
		return registration;
	}

	private static native void removeHandlerImpl(JavaScriptObject listener) /*-{
		$wnd.google.maps.event.removeListener(listener);
	}-*/;

	private static native JavaScriptObject addHandlerImpl(JavaScriptObject jso, String eventName, MClickHandler handler) /*-{
		var callback = function(event) {
			$entry(@com.sc.byahi.user.client.components.maps.MClickHandlerRegistration::onCallback(Lcom/sc/byahi/user/client/components/maps/MClickHandler;Lcom/google/gwt/ajaxloader/client/Properties;)(handler, event));
		};
		return $wnd.google.maps.event.addListener(jso, eventName, callback)
	}-*/;

}
